package com.scau.hwadee.examination.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.hwadee.examination.entity.*;
import com.scau.hwadee.examination.service.ProfessionalDeclarationService;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author YHX
 * @date 2019/8/3 14:24
 * description
 */
@Slf4j
@Controller
@RequestMapping("professional/declaration")
@Api(tags = "ProfessionalDeclarationController", description = "专业申报管理模块")
public class ProfessionalDeclarationController {

  @Autowired
  private ProfessionalDeclarationService professionalDeclarationService;

  @GetMapping("allMajor")
  @ApiOperation(value = "获取主考院校的所有专业")
  public ResponseEntity<ResponseVo> getAllMajor(@RequestParam("pageNum") int pageNum,
                                                @RequestParam("pageSize") int pageSize,
                                                @RequestParam("schoolId") String schoolId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取主考院校的所有专业");
    vo.setData(professionalDeclarationService.getAllMajorsOfSchool(pageNum, pageSize, schoolId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allCourse")
  @ApiOperation(value = "获取某专业的所有课程")
  public ResponseEntity<ResponseVo> getAllCourseOfMajor(@RequestParam("pageNum") int pageNum,
                                                        @RequestParam("pageSize") int pageSize,
                                                        @RequestParam("majorId") String majorId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取某专业的所有课程");
    vo.setData(professionalDeclarationService.getAllCourseOfMajor(pageNum, pageSize, majorId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("professionalPlanAdjustmentApplication")
  @ApiOperation(value = "专业计划调整申请")
  public ResponseEntity<ResponseVo> professionalPlanAdjustmentApplication(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("专业计划调整申请");
    Applicationprofessionalplanadjustment app = JsonUtils.JSONToObject(param, Applicationprofessionalplanadjustment.class);
    int cnt = professionalDeclarationService.insertApplicationprofessionalplanadjustment(app);
    String message;
    if (cnt > 0) {
      message = "专业计划调整申请成功";
    } else {
      message = "专业计划调整申请失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("professionalPlanAdjustmentCourse")
  @ApiOperation(value = "专业计划调整课程")
  public ResponseEntity<ResponseVo> professionalPlanAdjustmentCourse(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("专业计划调整课程");
    Majorplanadjustcourse majorplanadjustcourse = JsonUtils.JSONToObject(param, Majorplanadjustcourse.class);
    int cnt = professionalDeclarationService.insertMajorplanadjustcourse(majorplanadjustcourse);
    String message;
    if (cnt > 0) {
      message = "专业计划调整课程成功";
    } else {
      message = "专业计划调整课程失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allApprovedMajorRenewApproval")
  @ApiOperation(value = "获取所有的已通过的拟续办申请")
  public ResponseEntity<ResponseVo> getAllApprovedMajorRenewApproval(@RequestParam("pageNum") int pageNum,
                                                                     @RequestParam("pageSize") int pageSize,
                                                                     @RequestParam("schoolId") String schoolId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有的已通过的拟续办申请");
    vo.setData(professionalDeclarationService.getAllApprovedMajorRenewApproval(pageNum, pageSize, schoolId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("majorRenewApproval")
  @ApiOperation(value = "拟续办专业申请")
  public ResponseEntity<ResponseVo> majorRenewApproval(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("拟续办专业申请");
    JSONArray jsonArray = JsonUtils.JSONToObject(param, JSONObject.class).getJSONArray("jsonArray");
    int cnt = professionalDeclarationService.insertMajorRenewApprovalList(jsonArray);
    String message;
    if (cnt > 0) {
      message = "拟续办专业申请成功";
    } else {
      message = "拟续办专业申请失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allMajorExaminerIsNotTheSchoolId")
  @ApiOperation(value = "获取四川省已开办专业且该校尚未成为主考院校的专业列表")
  public ResponseEntity<ResponseVo> getAllMajorExaminerIsNotTheSchoolId(@RequestParam("pageNum") int pageNum,
                                                                        @RequestParam("pageSize") int pageSize,
                                                                        @RequestParam("schoolId") String schoolId) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("拟续办专业申请");
    vo.setData(professionalDeclarationService.listAllMajorExaminerIsNotTheSchoolId(pageNum, pageSize, schoolId));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("proposedExpansionOfProfessionalApplication")
  @ApiOperation(value = "拟扩办专业申请")
  public ResponseEntity<ResponseVo> proposedExpansionOfProfessionalApplication(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("拟扩办专业申请");
    String data = Objects.requireNonNull(JsonUtils.JSONToObject(param, JSONObject.class)).getJSONArray("jsonArray").toJSONString();
    int cnt = professionalDeclarationService.insertMajorExpandApprovalList(data);
    String message;
    if (cnt > 0) {
      message = "拟扩办专业申请成功";
    } else {
      message = "拟扩办专业申请失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("newProfessionalDeclaration")
  @ApiOperation(value = "提交新专业申报信息")
  public ResponseEntity<ResponseVo> createNewProfessionalDeclaration(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("提交新专业申报信息");
    Newmajordeclaration n = JsonUtils.JSONToObject(param, Newmajordeclaration.class);
    int cnt = professionalDeclarationService.insertNewProfessionalDeclaration(n);
    String message;
    if (cnt > 0) {
      message = "提交新专业申报信息成功";
    } else {
      message = "提交新专业申报信息失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("newCourseOptionalRelation")
  @ApiOperation(value = "新增专业课程选考关系")
  public ResponseEntity<ResponseVo> createnewCourseOptionalRelation(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("新增专业课程选考关系");
    Courseoptionalrelation c = JsonUtils.JSONToObject(param, Courseoptionalrelation.class);
    int cnt = professionalDeclarationService.insertNewMajorCourseOptionalRelation(c);
    String message;
    if (cnt > 0) {
      message = "新增专业课程选考关系成功";
    } else {
      message = "新增专业课程选考关系失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allApplicationprofessionalplanadjustment")
  @ApiOperation(value = "获取所有的专业计划调整信息")
  public ResponseEntity<ResponseVo> getAllApplicationprofessionalplanadjustment(@RequestParam("pageNum") int pageNum,
                                                                                @RequestParam("pageSize") int pageSize) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有的专业计划调整信息");
    vo.setData(professionalDeclarationService.getAllApplicationprofessionalplanadjustment(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("approvalOfProfessionalPlanAdjustment")
  @ApiOperation(value = "提交审批专业计划调整信息")
  public ResponseEntity<ResponseVo> submitApprovalOfProfessionalPlanAdjustment(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("提交审批专业计划调整信息");
    Applicationprofessionalplanadjustment c = JsonUtils.JSONToObject(param, Applicationprofessionalplanadjustment.class);
    int cnt = professionalDeclarationService.updateApplicationprofessionalplanadjustment(c);
    String message;
    if (cnt > 0) {
      message = "提交审批专业计划调整信息成功";
    } else {
      message = "提交审批专业计划调整信息失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allMajorrenewapproval")
  @ApiOperation(value = "获取所有的拟续办专业申请信息")
  public ResponseEntity<ResponseVo> getAllMajorrenewapproval(@RequestParam("pageNum") int pageNum,
                                                             @RequestParam("pageSize") int pageSize) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有的拟续办专业申请信息");
    vo.setData(professionalDeclarationService.getAllMajorrenewapproval(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("majorrenewapproval")
  @ApiOperation(value = "提交拟续办专业审批信息")
  public ResponseEntity<ResponseVo> submitMajorRenewApproval(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("提交拟续办专业审批信息");
    Majorrenewapproval c = JsonUtils.JSONToObject(param, Majorrenewapproval.class);
    int cnt = professionalDeclarationService.updateMajorrenewapproval(c);
    String message;
    if (cnt > 0) {
      message = "提交拟续办专业审批信息成功";
    } else {
      message = "提交拟续办专业审批信息失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allMajorExpandApproval")
  @ApiOperation(value = "获取所有的拟扩办专业申请信息")
  public ResponseEntity<ResponseVo> getAllMajorExpandApproval(@RequestParam("pageNum") int pageNum,
                                                              @RequestParam("pageSize") int pageSize) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有的拟扩办专业申请信息");
    vo.setData(professionalDeclarationService.getAllMajorexpandapproval(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("majorExpandApproval")
  @ApiOperation(value = "提交拟扩办专业审批信息")
  public ResponseEntity<ResponseVo> submitMajorExpandApproval(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("提交拟扩办专业审批信息");
    Majorexpandapproval c = JsonUtils.JSONToObject(param, Majorexpandapproval.class);
    int cnt = professionalDeclarationService.updateMajorexpandapproval(c);
    String message;
    if (cnt > 0) {
      message = "提交拟扩办专业审批信息成功";
    } else {
      message = "提交拟扩办专业审批信息失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("allNewMajorDeclaration")
  @ApiOperation(value = "获取所有的新专业申请信息")
  public ResponseEntity<ResponseVo> getAllNewMajorDeclaration(@RequestParam("pageNum") int pageNum,
                                                              @RequestParam("pageSize") int pageSize) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取所有的新专业申请信息");
    vo.setData(professionalDeclarationService.getAllNewMajorDeclaration(pageNum, pageSize));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping("newMajorDeclaration")
  @ApiOperation(value = "提交新专业申请审批信息")
  public ResponseEntity<ResponseVo> submitNewMajorDeclaration(@RequestBody String param) {
    ResponseVo vo = ResponseVo.instance();
    log.debug("提交新专业申请审批信息");
    Newmajordeclaration c = JsonUtils.JSONToObject(param, Newmajordeclaration.class);
    int cnt = professionalDeclarationService.updateNewMajorDeclaration(c);
    String message;
    if (cnt > 0) {
      message = "提交新专业申请审批信息成功";
    } else {
      message = "提交新专业申请审批信息失败";
    }
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }


}
